package com.tl.commonlibrary.ui.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.tl.commonlibrary.R;
import com.tl.commonlibrary.tool.DeviceUtil;

/**
 * Created by Administrator on 2017/6/23 0023.
 */

public class DialogInput extends Dialog implements View.OnClickListener {
    private int titleResId, hintResId;
    private int leftBtnTextResId, rightBtnTextResId;
    private TextView leftBtn, rightBtn, titleTView;
    private boolean leftDismiss = true, rightDismiss = true;
    private Context context;
    private EditText contentEText;

    private OnInputDialogClickListener listener;

    public DialogInput(Context context) {
        super(context, R.style.MyDialog);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_input);
        Window window = getWindow();
        WindowManager manager = window.getWindowManager();
        Display display = manager.getDefaultDisplay();
        WindowManager.LayoutParams params = window.getAttributes();
        Point point = new Point();
        display.getSize(point);
        params.height = Math.min(DeviceUtil.dp_to_px(200, getContext()), (int) (point.y * 0.5));
        params.width = Math.min(DeviceUtil.dp_to_px(300, getContext()), (int) (point.y * 2.0 / 3));
        window.setAttributes(params);
        titleTView = (TextView) findViewById(R.id.titleView);
        contentEText = (EditText) findViewById(R.id.contentEText);
        leftBtn = (TextView) findViewById(R.id.leftBtn);
        rightBtn = (TextView) findViewById(R.id.rightBtn);
        leftBtn.setOnClickListener(this);
        rightBtn.setOnClickListener(this);
        setLeftBtnText(leftBtnTextResId);
        setRightBtnText(rightBtnTextResId);
        setHint(hintResId);
        setTitle(titleResId);
        contentEText.setFocusable(true);
        contentEText.requestFocus();
        //设置显示dialog后自动弹出输入法
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

    }

    public String getContent() {
        return contentEText.getText().toString();
    }
    public EditText getContentView(){
        return contentEText;
    }
    public void addTextChangedListener(TextWatcher watcher) {
        contentEText.addTextChangedListener(watcher);
    }

    public void setLeftButtonName(int nameResId) {
        if (nameResId > 0 && leftBtn != null) {
            leftBtn.setText(nameResId);
        }
    }

    public void setRightButtonName(int nameResId) {
        if (nameResId > 0 && rightBtn != null) {
            rightBtn.setText(nameResId);
        }
    }

    public void setListener(OnInputDialogClickListener listener) {
        this.listener = listener;
    }
    public void setInputType(int inputType){
        if (contentEText != null) {
            contentEText.setInputType(inputType);
        }
    }
    public void setInputTypePassword(){
        setInputType(InputType.TYPE_CLASS_TEXT  | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    }
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.leftBtn) {
            if (leftDismiss) {
                dismiss();
            }
            if (listener != null) {
                listener.onLeftBtnClicked(v, getContent());
            }
        } else if (v.getId() == R.id.rightBtn) {
            if (rightDismiss) {
                dismiss();
            }
            if (listener != null) {
                listener.onRightBtnClicked(v, getContent());
            }
        }
    }

    public interface OnInputDialogClickListener {
        void onLeftBtnClicked(View view, String content);

        void onRightBtnClicked(View view, String content);
    }


    public void setTitle(int titleResId) {
        this.titleResId = titleResId;
        if (titleResId > 0 && titleTView != null) {
            titleTView.setText(titleResId);
        }
    }

    public void setHint(int hintResId) {
        this.hintResId = hintResId;
        if (hintResId > 0 && contentEText != null) {
            contentEText.setHint(hintResId);
        }
    }

    public void setLeftBtnText(int leftBtnTextResId) {
        this.leftBtnTextResId = leftBtnTextResId;
        if (leftBtnTextResId > 0 && leftBtn != null) {
            leftBtn.setText(leftBtnTextResId);
        }
    }

    public void setRightBtnText(int rightBtnTextResId) {
        this.rightBtnTextResId = rightBtnTextResId;
        if (rightBtnTextResId > 0 && rightBtn != null) {
            rightBtn.setText(rightBtnTextResId);
        }
    }

    public void setLeftDismiss(boolean leftDismiss) {
        this.leftDismiss = leftDismiss;
    }

    public void setRightDismiss(boolean rightDismiss) {
        this.rightDismiss = rightDismiss;
    }
}
